import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/Login.vue"),
  },
  {
    path: "/home",
    name: "home",
    redirect: '/index',
    component: () => import("../views/home/Home.vue"),
    children: [
      {
        path: "/index",
        name: "index",
        component: () => import("../views/index/Index.vue"),
        children: [
          {
            path: '/comic',
            name: 'comic',
            component: () => import('../views/index/comic/Comic.vue')
          }
        ]
      },
      {
        path: "/users",
        name: "用户列表",
        component: () => import("../views/users/Users.vue"),
      },
      {
        path: "/roles",
        name: '角色列表',
        component: () => import("@/views/power/Roles.vue")
      },
      {
        path: "/rights",
        name: '权限列表',
        component: () => import("@/views/power/Rights.vue")
      },
      {
        path: "/goods",
        name: '商品列表',
        component: () => import("@/views/goods/Goods.vue")
      },
      {
        path: "/add",
        name: '添加商品',
        component: () => import("@/views/goods/add.vue")
      },
      {
        path: "/params",
        name: '分类参数',
        component: () => import("@/views/goods/Params.vue")
      },
      {
        path: "/categories",
        name: '商品分类',
        component: () => import("@/views/goods/Categories.vue")
      },
      {
        path: "/orders",
        name: '订单列表',
        component: () => import("@/views/orders/Orders.vue")
      },
      {
        path: "/reports",
        name: '数据报表',
        component: () => import("@/views/reports/Reports.vue")
      },
    ]
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  //对/login直接放行
  if (to.fullPath === "/login") return next();
  //检查是否存有token
  const tokenstr = window.sessionStorage.getItem("token");
  //token为空视为未登录状态，跳转登录
  if (!tokenstr) return next("/login");
  //放行
  next();
});

export default router;
